package org.huyue.huyuexia.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.huyue.huyuexia.mapper.UserFollowMapper;
import org.huyue.huyuexia.mapper.UserMapper;
import org.huyue.huyuexia.pojo.Query.UserQueryParam;
import org.huyue.huyuexia.pojo.User;
import org.huyue.huyuexia.service.UserService;
import org.huyue.huyuexia.util.CurrentHolder;
import org.huyue.huyuexia.util.JwtUtils;
import org.huyue.huyuexia.util.pojo.LoginInfo;
import org.huyue.huyuexia.util.pojo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImpl  implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserFollowMapper userFollowMapper;
    @Override
    public PageResult findAll(UserQueryParam userQueryParam) {
        PageHelper.startPage(userQueryParam.getPageNum(), userQueryParam.getPageSize());
        List<User> users = userMapper.findAll();
        Page<User> page = (Page<User>) users;
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public void Delete(Integer id) {
        userMapper.Delete(id);
    }

    @Override
    public void add(User user) {
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userMapper.add(user);
    }

    @Override
    public void update(User user) {
        userMapper.Update(user);
    }

    @Override
    public LoginInfo login(User user) {
        log.info("用户:"+user.getUsername()+"登录");
        User u = userMapper.findByUsernameAndPassword(user.getUsername(), user.getPasswordHash());
        if(u!=null){
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("id", u.getId());
            dataMap.put("username", u.getUsername());

            String jwt = JwtUtils.generateJwt(dataMap);
            return new LoginInfo(u.getId(), u.getUsername(), u.getNickname(), jwt);
        }
        return null;
    }

    @Override
    public void register(String username, String password) {
        User u = new User();
        u.setUsername(username);
        u.setPasswordHash(password);
        userMapper.add(u);
    }

    @Transactional
    @Override
    public Boolean follow(Integer followId) {
        log.info("CurrentId: {}", CurrentHolder.getCurrentId());
        // 添加关注 自己 关注+1 阅豆+10  别人被关注 +1
        Integer userId = CurrentHolder.getCurrentId();
        User me = userMapper.findById(userId);
        me.setFollowerCount(me.getFollowingCount()+1);
        me.setBeanBalance(me.getBeanBalance()+10);
        User he = userMapper.findById(followId);
        he.setFollowingCount(he.getFollowerCount()+1);
        userMapper.Update(he);
        userMapper.Update(me);

        // 添加关注记录
        int i = userFollowMapper.selectUserFollowCount(userId, followId);
        if(i>0){
            userFollowMapper.insertUserFollow(userId, followId);
            return true;
        }else{
            //事务回滚
            throw new RuntimeException();
        }


    }

    @Override
    public List<Map> getArticleCount() {
        return userMapper.countArticle();
    }
}
